<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>用户历史记录</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <link rel="stylesheet" href="/static/layui/css/public.css">
    <script src="/static/layui/layui.js" charset="utf-8"></script>
    <script src="/static/js/axios.min.js"></script>
    <script src="/static/js/vue.js"></script>
    <script src="/static/js/jquery-3.6.0.js" type="text/javascript"></script>
    <script src="/static/js/jquery.cookie.js" type="text/javascript"></script>
</head>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">商品名</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="name" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">价格(≤)</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="price" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">浏览日期</label>
                                <div class="layui-input-inline">
                                    <input type="text" id="selectDatetime" name="datetime" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>

            <script type="text/html" id="formtoolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 批量删除 </button>
                </div>
            </script>

            <table class="layui-hide" id="formTableId" lay-filter="fileTableFilter"></table>

            <script type="text/html" id="fileTableBar">
                <a class="layui-btn layui-btn-normal layui-btn-xs layui-btn-warm " lay-event="show">查看</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>

            </script>

        </div>
    </div>
    <script>
        layui.use(['form', 'table', 'laydate'], function() {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                laydate = layui.laydate;


            laydate.render({
                elem: "#selectDatetime",
                type: "date",
                range: true
            })

            table.render({
                elem: '#formTableId',
                // url: '/static/api/table.json',
                url: '/userRecord/?type=history',
                toolbar: '#formtoolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [
                    [{
                        type: "checkbox",
                        width: 50
                    }, {
                        type: "numbers",
                        width: 50
                    }, {
                        field: 'name',
                        width: 300,
                        title: '商品名称'
                    }, {
                        field: 'price',
                        width: 80,
                        title: '价格',
                        sort: true
                    }, {
                        field: 'datetime',
                        width: 170,
                        title: '浏览时间',
                        sort: true
                    }, {
                        field: 'mall',
                        title: '商城',
                        sort: true
                    }, {
                        field: 'store',
                        title: '店铺',
                        sort: true
                    }, {
                        field: 'collection',
                        title: '是否收藏',
                        sort: true
                    }, {
                        field: 'link',
                        width: 80,
                        title: '链接',
                    }, {
                        title: '操作',
                        minWidth: 200,
                        toolbar: '#fileTableBar',
                        align: "center"
                    }]
                ],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true,
                skin: 'line'
            });

            // 监听搜索操作
            form.on('submit(data-search-btn)', function(data) {
                var result = JSON.stringify(data.field);
                // layer.alert(result, {
                //     title: '最终的搜索信息'
                // });

                //执行搜索重载
                table.reload('formTableId', {
                    page: {
                        curr: 1
                    },
                    where: {
                        searchParams: result
                    }
                }, 'data');

                return false;
            });

            /**
             * toolbar监听事件
             */
            table.on('toolbar(fileTableFilter)', function(obj) {
                if (obj.event === 'add') { // 监听添加操作
                    var index = layer.open({
                        title: '上传文件',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '../page/createQuestionForm.html',
                    });
                    $(window).on("resize", function() {
                        layer.full(index);
                    });
                } else if (obj.event === 'delete') { // 监听删除操作
                    var checkStatus = table.checkStatus('formTableId'),
                        data = checkStatus.data;
                    if (data.length == 0) {
                        layer.msg("请选中一个元素进行删除");
                        return false;
                    }
                    layer.confirm("确定要删除？", function(index) {
                        data0 = []
                        for (var i = 0; i < data.length; ++i) {
                            data0.push(data[i].id)
                        }

                        var cur = $(".layui-laypage-skip").find("input").val();
                        var limit = $(".layui-laypage-limits").find("option:selected").val();
                        let param = new URLSearchParams()
                        param.append('formID', data0)
                        axios.post('/delUserRecord/', param)
                            .then(function(response) {
                                console.log(response);
                                layui.table.reload('formTableId', {
                                    url: "/userRecord/?type=history",
                                    // type:"post",
                                    where: {
                                        page: cur,
                                        limit: limit
                                    },
                                    page: {
                                        curr: cur //重新从当前 页开始
                                    }
                                });


                                layer.msg("数据删除成功！", {
                                    icon: 1
                                });
                            }).catch(function(error) {
                                layer.msg("数据删除失败！", {
                                    icon: 2
                                });
                            });
                        layer.close(index);
                    });
                }
            });


            //监听表格复选框选择
            table.on('checkbox(fileTableFilter)', function(obj) {
                console.log(obj)
            });

            table.on('tool(fileTableFilter)', function(obj) {
                var data = obj.data;
                if (obj.event === 'edit') {

                    var index = layer.open({
                        title: '编辑表单信息',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '../page/table/edit_form.html',
                        success: function(layero, index) {
                            //打开子窗口后通过success赋值,两个变量基本是固定写法
                            var body = layer.getChildFrame('body', index);
                            var iframeWin = window[layero.find('iframe')[0]['name']];

                            console.log(data.content);

                            //给子窗口中的元素赋值
                            body.find('input[name="formID"]').val(data.formID);
                            body.find('input[name="formTitle"]').val(data.formTitle);
                            body.find('textarea[name="beizhu"]').val(data.beizhu);

                        }
                    });
                    $(window).on("resize", function() {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    layer.confirm('是否删除数据', function(index) {
                        // console.log(obj);
                        let param = new URLSearchParams()
                        param.append('formID', [obj.data.id])
                        axios.post('/delUserRecord/', param)
                            .then(function(response) {
                                obj.del();
                                layer.msg("数据删除成功！", {
                                    icon: 1
                                });
                            }).catch(function(error) {
                                layer.msg("数据删除失败！", {
                                    icon: 2
                                });
                            });
                        layer.close(index);

                    });
                } else if (obj.event === "show") {
                    console.log("event show", obj)
                    var index = layer.open({
                        title: '查看表单',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: data.link
                    });

                }
            });

        });
    </script>

</body>

</html>